﻿package com.devilcat.ob {
	
	import flash.display.MovieClip;
	import flash.events.Event;
	import flash.utils.clearInterval;
	import flash.utils.setInterval;
	import fl.transitions.Tween;
	import fl.transitions.easing.*;
	import fl.transitions.TweenEvent; 
	import com.devilcat.ws.OtherPlayerws;
	import com.devilcat.dt.Gamedt;
	
	
	public class OtherPlayer extends MovieClip {
		
		var wleft:Boolean=false;
		var wright:Boolean=false;
		var stand:Boolean=true;
		var walkvalue:int=0;
		var wsocket:OtherPlayerws;
		var intv:uint;
		public var tweenList:Array = new Array();
		public var uid:Number;
		public var disc:Boolean=false;
		public function OtherPlayer(_uid:int,username:String) {
			gotoAndPlay("standing");
			trace("Add Player "+_uid);
			uid=_uid;
			txtUsername.text=uid+"-"+username;
			wsocket=new OtherPlayerws(uid,this)
			intv = setInterval(checkPlayerStatus,100);
			this.addEventListener(Event.ENTER_FRAME,walk);
			// constructor code
		}
		function checkPlayerStatus(){
			wsocket.Update();
		}
		function walk(e:Event){
			this.x+=walkvalue;
		}
		public function Destroy(){
			this.removeEventListener(Event.ENTER_FRAME,walk);
			clearInterval(intv);
					tweenList.unshift(new Tween (this,"alpha",Strong.easeOut,1,0,2,true));//
//					tweenList.unshift(new Tween (this,"scaleX",Strong.easeOut,1,2,2,true));
//					tweenList.unshift(new Tween (this,"scaleY",Strong.easeOut,1,2,2,true));
//					tweenList.unshift(new Tween (this,"y",Strong.easeOut,this.y,this.y-50,2,true));
//					tweenList.unshift(new Tween (this,"x",Strong.easeOut,this.x,this.x+220,2,true));
					tweenList[0].addEventListener(TweenEvent.MOTION_FINISH,removeMe);
		}
		public function WalkLeft(){
			if(!wleft){
				gotoAndPlay("walking");
				wleft=true;
				wright=false;
				stand=false;
				scaleX = -1;
				walkvalue=-7;
			}			
		}
		public function WalkRight(){
			if(!wright){
				gotoAndPlay("walking");
				wleft=false;
				wright=true;
				stand=false;
				scaleX = 1;
				walkvalue=7;
			}			
		}
		public function WalkStand(){
			if(!stand){
				gotoAndPlay("standing");
				wleft=false;
				wright=false;
				stand=true;
				walkvalue=0;
			}			
		}
		public function removeMe(e:Event){
			if(this.parent != null){
				Gamedt.delOtherPlayer(uid);
				disc=true;
				this.parent.removeChild(this);
				//trace ("Removed");
			}
		}
	}
	
}
